package com.softstao.guoyu.ui.activity.sale;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.global.GsonManager;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.sale.RetailCondition;
import com.softstao.guoyu.model.shop.Goods;
import com.softstao.guoyu.model.shop.OrderProductInfo;
import com.softstao.guoyu.mvp.interactor.sale.RetailInteractor;
import com.softstao.guoyu.mvp.presenter.sale.RetailPresenter;
import com.softstao.guoyu.mvp.viewer.sale.AddRetailViewer;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewBaseAdapter;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewHolder;
import com.softstao.softstaolibrary.library.utils.LZUtils;
import com.softstao.softstaolibrary.library.widget.CustomScrollerView;
import com.softstao.softstaolibrary.library.widget.FullyLinearLayoutManager;
import com.softstao.softstaolibrary.library.widget.LZToast;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2017/7/11.
 */

public class AddRetailActivity extends BaseActivity implements AddRetailViewer{

    @AIPresenter(presenter = RetailPresenter.class,interactor = RetailInteractor.class)
    RetailPresenter presenter;

    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.wechat)
    EditText wechat;
    @BindView(R.id.mobile)
    EditText mobile;
    @BindView(R.id.order_price)
    TextView orderPrice;
    @BindView(R.id.shipping_method)
    EditText shippingMethod;
    @BindView(R.id.shipping_num)
    EditText shippingNum;
    @BindView(R.id.list_view)
    RecyclerView listView;
    @BindView(R.id.scroll_view)
    CustomScrollerView scrollView;
    @BindView(R.id.loading)
    RelativeLayout loading;
    private RetailCondition condition = new RetailCondition();
    private List<OrderProductInfo> orderProductInfos = new ArrayList<>();
    private List<Goods> goodsList = new ArrayList<>();
    private double price;
    private RecycleViewBaseAdapter<Goods> adapter;

    @Override
    public int _ContentView() {
        return R.layout.activity_add_retail;
    }

    @Override
    public void initView() {
        initTitle("填写零售单信息");
        goodsList = SharePreferenceManager.getInstance().getChooseGoods();

        for (int i=0; i<goodsList.size(); i++){
            price += goodsList.get(i).getPrice()*goodsList.get(i).getNum();

            OrderProductInfo orderProductInfo = new OrderProductInfo();
            orderProductInfo.setProductId(goodsList.get(i).getProductId());
            orderProductInfo.setCount(goodsList.get(i).getNum());
            orderProductInfos.add(orderProductInfo);
        }
        orderPrice.setText(getResources().getString(R.string.rmb) + price);

        adapter = new RecycleViewBaseAdapter<Goods>(goodsList,R.layout.layout_order_confirm_item) {
            @Override
            public void convert(RecycleViewHolder holder, Goods goods) {
                RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                holder.itemView.setLayoutParams(layoutParams);
                if (goods.getImg()!=null&&!goods.getImg().equals("")){
                    Glide.with(context).load(goods.getImg()).into((ImageView) holder.getView(R.id.img));
                }else {
                    ((ImageView) holder.getView(R.id.img)).setImageResource(R.mipmap.default_im);
                }
                holder.setText(R.id.name,goods.getName())
                        .setText(R.id.price,getResources().getString(R.string.rmb) + LZUtils.priceFormat(goods.getPrice()))
                        .setText(R.id.num,"x" + goods.getNum())
                        .setText(R.id.total_price,getResources().getString(R.string.rmb)+ LZUtils.priceFormat(goods.getPrice()*goods.getNum()));
            }
        };
        listView.setAdapter(adapter);
        listView.setLayoutManager(new FullyLinearLayoutManager(this));
        adapter.notifyDataSetChanged();
    }

    @OnClick(R.id.submit_btn)
    public void onViewClicked() {
        if (!check()){
            return;
        }
        addRetail();
    }

    private boolean check(){
        if (TextUtils.isEmpty(name.getText())){
            LZToast.getInstance(context).showToast("请输入买家姓名");
            return false;
        }
        return true;
    }

    @Override
    public void addRetail() {
        loading.setVisibility(View.VISIBLE);
        condition.setAgentId(SharePreferenceManager.getInstance().getAgentId());
        condition.setName(name.getText().toString());
        condition.setWechat(wechat.getText().toString());
        condition.setMobile(mobile.getText().toString());
        condition.setMode(shippingMethod.getText().toString());
        condition.setLogisticsCode(shippingNum.getText().toString());
        condition.setProductInfo(GsonManager.getInstance().getGson().toJson(orderProductInfos));
        presenter.addRetail(condition);
    }

    @Override
    public void addResult(Object result) {
        loading.setVisibility(View.GONE);
        LZToast.getInstance(context).showToast("提交成功");
        finish();
    }

    @Override
    protected void error(String msg) {
        super.error(msg);
        loading.setVisibility(View.GONE);
    }
}
